<?php

/**
 * This is the model class for table "users".
 *
 * The followings are the available columns in table 'users':
 * @property string $UserId
 * @property string $FullName
 * @property string $Password
 * @property string $Email
 * @property string $SuperiorEmail
 * @property integer $GroupId
 * @property integer $DepartmenId
 * @property boolean $Enable
 * @property string $CreateAt
 * @property string $UpdateAt
 * @property boolean $IsSuperAdmin
 * @property string $Role
 * @property boolean $DeleteFlg
 * @property string $Salt
 * @property string $CompanyId
 *
 * The followings are the available model relations:
 * @property Expense[] $expenses
 */
class User extends CActiveRecord {
	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array( 
		array('FullName,Email,Password', 'required'),
		array('Email','email'),
		array('SuperiorEmail','email'),
		array('GroupId, DepartmenId', 'numerical', 'integerOnly' => true), array('FullName, Email, SuperiorEmail', 'length', 'max' => 255), array('Password, Salt', 'length', 'max' => 50), array('Role', 'length', 'max' => 1024), array('CompanyId', 'length', 'max' => 19), array('Enable, CreateAt, UpdateAt, IsSuperAdmin, DeleteFlg', 'safe'),
		// The following rule is used by search().
		// @todo Please remove those attributes that should not be searched.
		array('UserId, FullName, Password, Email, SuperiorEmail, GroupId, DepartmenId, Enable, CreateAt, UpdateAt, IsSuperAdmin, Role, DeleteFlg, Salt, CompanyId', 'safe', 'on' => 'search'), );
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array('expenses' => array(self::HAS_MANY, 'Expense', 'UserId'), );
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array('UserId' => 'User', 'FullName' => 'Full Name', 'Password' => 'Password', 'Email' => 'Email', 'SuperiorEmail' => 'Superior Email', 'GroupId' => 'Group', 'DepartmenId' => 'Departmen', 'Enable' => 'Enable', 'CreateAt' => 'Create At', 'UpdateAt' => 'Update At', 'IsSuperAdmin' => 'Is Super Admin', 'Role' => 'Role', 'DeleteFlg' => 'Delete Flg', 'Salt' => 'Salt', 'CompanyId' => 'Company', );
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search() {
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria = new CDbCriteria;

		$criteria -> compare('UserId', $this -> UserId, true);
		$criteria -> compare('FullName', $this -> FullName, true);
		$criteria -> compare('Password', $this -> Password, true);
		$criteria -> compare('Email', $this -> Email, true);
		$criteria -> compare('SuperiorEmail', $this -> SuperiorEmail, true);
		$criteria -> compare('GroupId', $this -> GroupId);
		$criteria -> compare('DepartmenId', $this -> DepartmenId);
		$criteria -> compare('Enable', $this -> Enable);
		$criteria -> compare('CreateAt', $this -> CreateAt, true);
		$criteria -> compare('UpdateAt', $this -> UpdateAt, true);
		$criteria -> compare('IsSuperAdmin', $this -> IsSuperAdmin);
		$criteria -> compare('Role', $this -> Role, true);
		$criteria -> compare('DeleteFlg', $this -> DeleteFlg);
		$criteria -> compare('Salt', $this -> Salt, true);
		$criteria -> compare('CompanyId', $this -> CompanyId, true);

		return new CActiveDataProvider($this, array('criteria' => $criteria, ));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	// hash password
	public function hashPassword($password, $salt) {
		return md5($salt . $password);
	}

	// password validation
	public function validatePassword($password) {
		return $this -> hashPassword($password, $this -> Salt) === $this -> Password;
	}

	//generate salt
	public function generateSalt() {
		return uniqid('', true);
	}

	public function beforeValidate() {
		$this -> Salt = $this -> generateSalt();
		return parent::beforeValidate();
	}

	public function beforeSave() {
		$this -> Password = $this -> hashPassword($this -> Password, $this -> Salt);
		return parent::beforeSave();
	}

}
